﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using QuanLyKho.GUI.Library;
using QuanLyKho.Common;
using QuanLyKho.DataAccess;
using QuanLyKho.GUI.Frm;
using System.Xml;

namespace QuanLyKho.GUI
{
    public partial class ucListMachinery : UserControl
    {
        private int _ID = 0;
        private long _ReID = 0;
        frmListMachinery frm = null;
        private DataSet dsTemp = new DataSet();
        private string NavClicked = "";
        private int totalRows = 25; //Tong so danh sach muon hien thi len DataGridView
        static public long ID = 0;
        public ucListMachinery()
        {
            InitializeComponent();
            binDataListMachinery();
        }
        private enum NavButton
        {
            First = 1,
            Next = 2,
            Previous = 3,
            Last = 4,
        }

       

        private void ShowForm(long _ID)
        {
            if (_ID < 1)
                frm = new frmListMachinery();
            else
                frm = new frmListMachinery(_ID);
            frm.StartPosition = FormStartPosition.CenterParent;
            frm.ShowInTaskbar = false;
            frm.MaximizeBox = false;
            frm.MinimizeBox = false;
            frm.ShowDialog();
            binDataListMachinery();
        }
        private void binDataListMachinery()
        {
            List<clsListMachinery> listMachinery = new clsDataAccessListMachinery().ListMachinerySelectAll();
            DataTable dt = new DataTable("User");
            if (listMachinery.Count == 0)
            {
                grdListMachinery.DataSource = dt;
                return;
            }
            else
            {
                dt.Columns.Add("Check", typeof(CheckBox));
                dt.Columns.Add("ID", typeof(long));
                dt.Columns.Add("STT", typeof(int));
                dt.Columns.Add("Mã", typeof(string));
                dt.Columns.Add("Tên Máy Móc", typeof(string));
                dt.Columns.Add("Mô tả", typeof(string));
                DataRow dr;
                int stt = 0;
                foreach (clsListMachinery obj in listMachinery)
                {
                    dr = dt.NewRow();
                    stt = stt + 1;
                    dr[0] = (CheckBox)new CheckBox();
                    dr[1] = obj.ID;
                    dr[2] = stt;
                    dr[3] = obj.Code;
                    dr[4] = obj.Name;
                    dr[5] = obj.Des;
                    dt.Rows.Add(dr);
                }
                DataSet ds = new DataSet();
                ds.Tables.Add(dt);

                dsTemp = ds;
                if (ds.Tables[0].Rows.Count > 0)
                    new clsPaging().fillDataGrid(ds, bindingSource1, grdListMachinery, "", lblPaging, totalRows);
                else
                {
                    bindingSource1.DataSource = null;
                    bindingSource1.Clear();
                    grdListMachinery.DataSource = bindingSource1;
                }

                if (grdListMachinery.ColumnCount > 0)
                {
                    grdListMachinery.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
                    grdListMachinery.Columns[1].Visible = false;
                    grdListMachinery.Columns[2].Visible = false;
                    grdListMachinery.Columns[3].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
                    grdListMachinery.Columns[4].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
                    grdListMachinery.Columns[5].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
                }
            }
        }

        private void chlAll_CheckedChanged(object sender, EventArgs e)
        {
            if (chkCheckAll.Checked == true)
            {
                foreach (DataGridViewRow row in grdListMachinery.Rows)
                {
                    ((DataGridViewCheckBoxCell)row.Cells["Status"]).Value = true;
                }
            }
            else
            {
                foreach (DataGridViewRow row in grdListMachinery.Rows)
                {
                    ((DataGridViewCheckBoxCell)row.Cells["Status"]).Value = false;
                }
            }
        }

        private void butAddNew_Click(object sender, EventArgs e)
        {
           ShowForm(0);
        }

        private void butUpdate_Click(object sender, EventArgs e)
        {
            int i = 0;
            long ID;

            foreach (DataGridViewRow row in grdListMachinery.Rows)
            {
                if ((Convert.ToInt32(row.Cells["status"].Value) == 1) && (Convert.ToInt64(grdListMachinery.CurrentRow.Cells["status"].Value) == 1))
                    i++;

            }
            if (i > 1)
            {
                new clsLibrary().runLoading("Chọn một tên máy muốn sửa!", 0);
                HavitelMessageBox.Show("Chỉ được chọn một thiết bị!", "Thông báo", HavitelMessageBox.HavitelMessageButton.OK, HavitelMessageBox.HavitelMessageIcon.Warning);
            }
            if (i == 1)
            {
                ID = Convert.ToInt64(grdListMachinery.CurrentRow.Cells["ID"].Value.ToString());
                ShowForm(ID);
                new clsLibrary().runLoading("Đã sửa thành công!", 2);

            }
            if (i == 0)
            {
                new clsLibrary().runLoading("Chọn tên thiết bị muốn sửa!", 0);
                HavitelMessageBox.Show("Chọn tên thiết bị muốn sửa!", "Thông báo", HavitelMessageBox.HavitelMessageButton.OK, HavitelMessageBox.HavitelMessageIcon.Warning);
            }
            binDataListMachinery();
        }

        private void butDelete_Click(object sender, EventArgs e)
        {
            clsDataAccessListMachinery listDataAccessListMachinery = new clsDataAccessListMachinery();
            if (Convert.ToInt64(grdListMachinery.CurrentRow.Cells["status"].Value) == 0)
            {
                new clsLibrary().runLoading("Chọn tên thiết bị muốn xóa!", 0);
                HavitelMessageBox.Show("Chọn tên thiết bị muốn xóa!", "Thông báo", HavitelMessageBox.HavitelMessageButton.OK, HavitelMessageBox.HavitelMessageIcon.Warning);
            }
            else
            {
                if (HavitelMessageBox.Show("Bạn có muốn xóa thông tin thiết bị này?", "Thông báo", HavitelMessageBox.HavitelMessageButton.OKCancel, HavitelMessageBox.HavitelMessageIcon.Question) == DialogResult.OK)
                {
                    foreach (DataGridViewRow row in grdListMachinery.Rows)
                    {
                        if (Convert.ToInt32(row.Cells["status"].Value) == 1)
                        {
                            long ID = Convert.ToInt64(row.Cells["ID"].Value.ToString());
                            listDataAccessListMachinery.Delete(ID);
                            new clsLibrary().runLoading("Đã xóa thành công!", 2);
                        }

                    }
                }
                else
                {
                    binDataListMachinery();
                }
            }
            binDataListMachinery();
        }

        private void butAddedXml_Click(object sender, EventArgs e)
        {
            OpenFileDialog Open = new OpenFileDialog();
            DataTable dt = new DataTable();
            if (Open.ShowDialog() == DialogResult.OK)
            {
                XmlDataDocument xmlDatadoc = new XmlDataDocument();
                xmlDatadoc.DataSet.ReadXml("" + Open.FileName + "");
                DataSet dsXml = new DataSet("List");
                dsXml = xmlDatadoc.DataSet;
                dt.Columns.Add("Check", typeof(CheckBox));
                dt.Columns.Add("ID", typeof(long));
                dt.Columns.Add("STT", typeof(int));
                dt.Columns.Add("Mã máy móc", typeof(string));
                dt.Columns.Add("Tên máy móc", typeof(string));
                dt.Columns.Add("Ghi chú", typeof(string));               
                DataRow dr;
                int stt = 0;
                foreach (DataRow Row in dsXml.Tables[0].Rows)
                {
                    dr = dt.NewRow();
                    stt = stt + 1;
                    dr[0] = (CheckBox)new CheckBox();
                    dr[1] = Row["ID"].ToString();
                    dr[2] = stt;
                    dr[3] = Row["Code"].ToString();
                    dr[4] = Row["Name"].ToString();
                    dr[5] = Row["Des"].ToString();                   
                    dt.Rows.Add(dr);
                }
                DataSet ds = new DataSet();
                ds.Tables.Add(dt);
                dsTemp = ds;
                if (ds.Tables[0].Rows.Count > 0)
                    new clsPaging().fillDataGrid(ds, bindingSource1, grdListMachinery, "", lblPaging, totalRows);
                else
                {
                    bindingSource1.DataSource = null;
                    bindingSource1.Clear();
                    grdListMachinery.DataSource = bindingSource1;
                }
                if (grdListMachinery.ColumnCount > 0)
                {
                    grdListMachinery.Columns[1].Visible = false;
                    grdListMachinery.Columns[2].Visible = false;//
                    grdListMachinery.Columns[3].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;//stt
                    grdListMachinery.Columns[4].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;//Mavt
                    grdListMachinery.Columns[5].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;//TenVT
                    grdListMachinery.Columns[6].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;//GHiChu
                   
                }
            }
        }

        private void butExport_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "Text files (*.xml)|*.xml|All files (*.*)|*.*";
            saveFileDialog.Title = "Chọn file XML";
            List<clsListMachinery> ListMachinery = new clsDataAccessListMachinery().ListMachinerySelectAll();
            DataTable dt = new DataTable("listMaterial");
            dt.Columns.Add("ID", typeof(long));
            dt.Columns.Add("Code", typeof(string));
            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("Des", typeof(string));
            DataRow dtrow;
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                foreach (clsListMachinery obj in ListMachinery)
                {
                    dtrow = dt.NewRow();
                    dtrow[0] = obj.ID;
                    dtrow[1] = obj.Code;
                    dtrow[2] = obj.Name;
                    dtrow[3] = obj.Des;
                    dt.Rows.Add(dtrow);
                }
                DataSet ds = new DataSet("ListMaterial");
                ds.Tables.Add(dt);
                ds.WriteXml(saveFileDialog.FileName, XmlWriteMode.IgnoreSchema);
                new clsLibrary().runLoading("Đã lưu dữ liệu ra file : '" + saveFileDialog.FileName + "'.", 2);
            }
        }

        private void butFirst_Click(object sender, EventArgs e)
        {
            NavClicked = NavButton.First.ToString();
            new clsPaging().fillDataGrid(dsTemp, bindingSource1, grdListMachinery, NavClicked, lblPaging, totalRows);
        }

        private void butPrevious_Click(object sender, EventArgs e)
        {
            NavClicked = NavButton.Previous.ToString();
            new clsPaging().fillDataGrid(dsTemp, bindingSource1, grdListMachinery, NavClicked, lblPaging, totalRows);
        }

        private void butNext_Click(object sender, EventArgs e)
        {
            NavClicked = NavButton.Next.ToString();
            new clsPaging().fillDataGrid(dsTemp, bindingSource1, grdListMachinery, NavClicked, lblPaging, totalRows);
        }

        private void butLast_Click(object sender, EventArgs e)
        {
            NavClicked = NavButton.Last.ToString();
            new clsPaging().fillDataGrid(dsTemp, bindingSource1, grdListMachinery, NavClicked, lblPaging, totalRows);
        }
    }
}
